package com.eascs.web.o2o.web.reward.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.o2o.web.reward.entity.PersonalInfo;
import com.eascs.web.o2o.web.reward.entity.Province;
import com.eascs.web.o2o.web.reward.entity.RewardDataDic;
import com.eascs.web.o2o.web.reward.entity.RewardItem;
import com.eascs.web.o2o.web.reward.entity.SearchStaffForm;
import com.eascs.web.o2o.web.reward.service.BaseService;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.vo.ModelResult;

/**
 * 考核人员
 * @author EA
 *
 */
@RestController
@RequestMapping("/reward")
public class RewardRestController  extends BaseRestController {

	private static Logger logger = LoggerFactory.getLogger(RewardRestController.class);
	
	@Inject
	BaseService baseService;
	
	/**
	 * 根据考核类型联动考核方案
	 * @param assesPro
	 * @return
	 */
	@RequestMapping("/updateAssesPro")
	public HtmlRestResult updateAssesProByAssesType(String evaluateType,String provinceCode,Date joinDate){
		logger.info("联动考核方案接口入参,evaluateType:{},provinceCode:{},joinDate:{}" , evaluateType ,provinceCode,joinDate);
		evaluateType = evaluateType.split("\\|")[0];
		
		String vEvaluateScheme = baseService.getEvaluateScheme(evaluateType, provinceCode.split("\\|")[0], joinDate);
		List<RewardItem> ris = baseService.findRewardItems(evaluateType);
		List<Object[]> los = new ArrayList<Object[]>();
		java.util.Map<String, String> itemMap = new HashMap<>();
		for(RewardItem item : ris){
			Object[] os = new Object[2];
			os[0] = item.getNo();//考核方案名称
			os[1] = item.getName();//考核方案名称
			itemMap.put((String)os[0], (String)os[1]);
			los.add(os);
		}
		ViewContext.put("los", los);
		ViewContext.put("defaultEvaluateSchemeCode", vEvaluateScheme);
		ViewContext.put("defaultEvaluateSchemeName", itemMap.get(vEvaluateScheme));
		return HtmlRestResult.html("reward/getAssesProHtml");
	}
	
	@RequestMapping("/delStaff")
	public BaseRestResult<String> delStaff(PersonalInfo pi){
		logger.debug("删除人员信息入参：{}", JSON.toJSONString(pi));
		int result = baseService.delPersionInfo(pi);
		String msg = result == 0? "删除失败!" :"删除成功!";
		return BaseRestResult.info(msg).andRefresh();
	}
	
    /**
     * 编辑保存
     * @param pi
     * @return
     */
	@RequestMapping("/editAndSaveStaff")
	public BaseRestResult<String> editAndSaveStaff(PersonalInfo pi){
		String provinceCode = pi.getProvinceCode();
		if(!"请选择".equals(provinceCode)){
			pi.setProvinceCode(provinceCode.split("\\|")[0]);
			pi.setProvinceName(provinceCode.split("\\|")[1]);
		}else{
			pi.setProvinceCode(null);
			pi.setProvinceName(null);
		}
		
		pi.setUpdateUser(UserHolder.getUser().getAccount());
		pi.setUpdate_date(new Date(System.currentTimeMillis()));
		
		logger.debug("编辑人员信息保存入参：{}", JSON.toJSONString(pi));
		if(pi.getUserName() == null)
			return BaseRestResult.warn("编辑人员姓名不能为空!");
		
		if("请选择".equals(pi.getEvaluateType()))
			return BaseRestResult.warn("编辑人员考核类型不能为空!");
		
		if("请选择".equals(pi.getEvaluateScheme()))
			return BaseRestResult.warn("编辑人员考核方案不能为空!");
		
		boolean flag = baseService.editAndSavePersonalInfo(pi);
		return BaseRestResult.info(flag?"保存成功":"保存失败").andRedirect("./searchStaffInfo?userName="+pi.getUserName());
	}
	
	
	
	/**
	 * 新增保存
	 * @param pi
	 * @return
	 */
	@RequestMapping("/addAndSaveStaff")
	public BaseRestResult<String> addAndSaveStaff(PersonalInfo pi){
		pi.setCreateUser(UserHolder.getUser().getAccount());
		pi.setCreateDate(new Date(System.currentTimeMillis()));
		logger.debug("新增人员信息保存入参：{}", JSON.toJSONString(pi));
		if(pi.getUserName() == null)
			return BaseRestResult.warn("新增人员姓名不能为空!");
		
		if("请选择".equals(pi.getEvaluateType()))
			return BaseRestResult.warn("新增人员考核类型不能为空!");
		
		if("请选择".equals(pi.getEvaluateScheme()))
			return BaseRestResult.warn("新增人员考核方案不能为空!");
		
		String provinceCode = pi.getProvinceCode();
		if("请选择".equals(provinceCode)){
			pi.setProvinceCode(null);
			pi.setProvinceName(null);
		}else if(StringUtil.isNotBlank(provinceCode)){
			pi.setProvinceCode(provinceCode.split("\\|")[0]);
			pi.setProvinceName(provinceCode.split("\\|")[1]);
		}
		boolean flag = baseService.addAndSavePersonalInfo(pi);
		return BaseRestResult.info(flag?"新增信息保存成功":"新增信息保存失败").andRedirect("./searchStaffInfo?userName="+pi.getUserName());
	}
	
	/**
	 * 根据人名模糊搜索金服平台人员信息
	 * @param form
	 * @return
	 */
	@RequestMapping("/likeUserName")
	public BaseRestResult<List<Object[]>> likeUserName(SearchStaffForm form){
		logger.debug("联想考核人员姓名入参：{}", form.getUserName());
		List<Object[]> data = new ArrayList<>();
		if (StringUtil.hasText(form.getUserName())) {
			ModelResult<List<Object[]>> da = baseService.findListByLikeUserName(form.getUserName());
			data = da.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	
	@RequestMapping("/toEditStaff")
	public BaseRestResult<String> editStaff(SearchStaffForm form){
		logger.debug("编辑人员信息入参：{}", JSON.toJSONString(form));
		String userNo = form.getUserNo();
		String tmonth = form.getTmonth();
		PersonalInfo pi = baseService.findPersonalInfo(userNo,tmonth);
		List<Object[]> lobs = baseService.findPersonalByUserNo(userNo,tmonth);
		Object[] obs = lobs.get(0);
		ViewContext.put("dd_no", obs[2]);
		ViewContext.put("dd_name", obs[3]);
		ViewContext.put("lobs", lobs);
		ViewContext.put("personalInfo", pi);
		
		List<Province> provinces = baseService.findAllEntity(Province.class);
		ViewContext.put("provinces", provinces);
		//去掉省份中已默认的选项
		for(Province province : provinces){
			if(province.getCode().equals(pi.getProvinceCode())){
				provinces.remove(province);
				break;
			}
		}
		
		List<RewardDataDic> dics = baseService.findRewardDataDics();
		//去掉考核类型中相应已默认的选项
		for(RewardDataDic dic : dics){
			if(dic.getNo().equals(obs[2])){
				dics.remove(dic);
				break;
			}
		}
		ViewContext.put("dics", dics);
		return HtmlRestResult.popHtmlFragment("reward/edit_staff");
	}
	
	
	/**
	 * 编辑确认
	 * @param pi
	 * @return
	 */
	@RequestMapping("/editAndConfirmStaff")
	public BaseRestResult<String> editAndConfirmStaff(PersonalInfo pi){
		String provinceCode = pi.getProvinceCode();
		if(!"请选择".equals(provinceCode)){
			pi.setProvinceCode(provinceCode.split("\\|")[0]);
			pi.setProvinceName(provinceCode.split("\\|")[1]);
		}else{
			pi.setProvinceCode(null);
			pi.setProvinceName(null);
		}
		pi.setConfirmUser(UserHolder.getUser().getAccount());
		pi.setConfirmDate(new Date(System.currentTimeMillis()));
		logger.debug("编辑人员信息确认入参：{}", JSON.toJSONString(pi));
		boolean flag = baseService.editAndConfirmPersonalInfo(pi);
		
		return BaseRestResult.info(flag?"确认成功":"确认失败").andRedirect("./searchStaffInfo?userName="+pi.getUserName());
	}
	
}
